package com.thermoberg.passport.controllor;

import com.thermoberg.passport.entity.SbyPassport;
import com.thermoberg.passport.service.SbyPassportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wanghe on 2016/11/3.
 */
@Controller
@RequestMapping(value = "/personal")
public class PersonInfomationController {
    @Autowired
    private SbyPassportService sbyPassportService;


    private final String FAIL_PWD = "{\"code\": 2, \"msg\": \"原密码不正确！\"}";
    private final String RETURN_PWD = "{\"code\": 0, \"url\": \"/personal/updatepwd\"}";

    @RequestMapping(value = "/infomation", method = RequestMethod.GET)
    public String infomation(ModelMap model ,HttpServletRequest request) {
        Integer id = Integer.parseInt((String) request.getAttribute("id"));
        SbyPassport sbyPassport=sbyPassportService.sbyPassportList(id);
        model.addAttribute("sbyPassport", sbyPassport);
        return "personal_infomation";
    }

    @RequestMapping(value = "/upPwdView", method = RequestMethod.GET)
    public String upPwdView() {
        return "update_password";
    }

    @RequestMapping(value = "/querypwd", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public
    @ResponseBody
    String querypwd(@RequestParam String mypwd, HttpServletRequest request) {
        Integer id = Integer.parseInt((String) request.getAttribute("id"));
        SbyPassport querypassword = sbyPassportService.querypwd(id, mypwd);
        if (querypassword == null) {
            return FAIL_PWD;
        }
        return RETURN_PWD;
    }

    @RequestMapping(value = "/updatepwd", method = RequestMethod.GET)
    public String updatepwd(@RequestParam String newpwd, HttpServletRequest request) {
        Integer id = Integer.parseInt((String) request.getAttribute("id"));
        sbyPassportService.updatepwd(id, newpwd);
        return "redirect:/account/dologout";
    }

}
